package com.renwei.yunlong.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.adapter.OwnerContentsAdapter;
import com.renwei.yunlong.adapter.OwnerSelectNewAdapter;
import com.renwei.yunlong.adapter.ServiceContentsAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.OwnerContentsBean;
import com.renwei.yunlong.bean.ServiceContentsBean;
import com.renwei.yunlong.bean.LoginBean;
import com.renwei.yunlong.bean.OwnerSelectNewBean;
import com.renwei.yunlong.bean.SelectBelongsBean;
import com.renwei.yunlong.bean.ServiceLoginBean;
import com.renwei.yunlong.http.HttpPresenter;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.ACache;
import com.zyf.fwms.commonlibrary.http.Api;
import com.zyf.fwms.commonlibrary.utils.AutoUtils;
import com.zyf.fwms.commonlibrary.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static android.R.attr.id;
import static android.R.attr.name;

/**
 * 服务目录 - - 页面
 * <p>
 * Created by Administrator on 2017/8/31.
 */

public class ServiceContentsActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;

    private ACache mCache;

    private OwnerContentsAdapter ownerAdapter;
    private ServiceContentsAdapter serviceAdapter;

    String companyType;
    String companyCode;
    String contractId;
    String hardwareId;
    LoginBean loginBean;
    ServiceLoginBean serviceBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_select_belongs);
        ButterKnife.bind(this);
        mCache = ACache.get(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        //屏幕自适配
        AutoUtils.setSize(this, false, 720, 1280);
        AutoUtils.auto(this);

        initData();
    }

    private void initData() {
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(this);
        tvTitle.setText("选择所属服务");

        companyType = mCache.getAsString("companyType");
        companyCode = getIntent().getStringExtra("companyCode");
        contractId = getIntent().getStringExtra("contractId");
        hardwareId = getIntent().getStringExtra("serviceHardwareId");
        loginBean = (LoginBean) mCache.getAsObject("loginBean");

        if ("1".equals(companyType)) {//业主
            String jsonBean = "{ownerCode:\"" + loginBean.getRows().getOwnerCode() +"\"}";
            ownerSelectNewUnitHttp(jsonBean);
            Log.e("业主服务目录jsonBean",jsonBean);

        } else if ("2".equals(companyType)) {//服务商
            //{"contractId":选择所属项目ID,"hardwareId":选择服务对象ID,"ownerCode":选择发起企业Code, " serviceProviderCode ":当前公司编码, "currentUserId":当前员工employeeId }
            serviceBean = (ServiceLoginBean) mCache.getAsObject("serviceLoginBean");
            String jsonBean = "{contractId:\"" + contractId + "\",hardwareId:\"" + hardwareId +"\",ownerCode:\"" + companyCode + "\",serviceProviderCode:\"" +serviceBean.getRows().getServiceProviderCode()+"\",currentUserId:\"" +serviceBean.getRows().getEmployeeId()+ "\"}";
            serviceSelectNewUnitHttp(jsonBean);
            Log.e("服务商服务目录jsonBean",jsonBean);
        }
    }

    private void setOwnerListDate(List<OwnerContentsBean.Rows> mList) {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        ownerAdapter = new OwnerContentsAdapter(ServiceContentsActivity.this, mList);
        ownerAdapter.setOnItemClickListener(new OwnerContentsAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, String directoryName,int directoryId) {
                Intent intent = new Intent();
                intent.putExtra("directoryName",directoryName);
                mCache.put("directoryId",String.valueOf(directoryId));
                setResult(RESULT_OK, intent);
                ownerAdapter.setSelection(position);
                ownerAdapter.notifyDataSetChanged();
                ServiceContentsActivity.this.finish();
            }
        });
        recyclerView.setAdapter(ownerAdapter);
    }

    private void setServiceListDate(List<ServiceContentsBean.Rows> mList) {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        serviceAdapter = new ServiceContentsAdapter(ServiceContentsActivity.this,mList);
        serviceAdapter.setOnItemClickListener(new ServiceContentsAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, String contractName,String completeTimes,String type,String detailId,String relationId) {
                Intent intent = new Intent();
                intent.putExtra("contractName",contractName);
                intent.putExtra("completeTimes",completeTimes);
                intent.putExtra("type",type);
                mCache.put("detailId",detailId);
                mCache.put("relationId",relationId);
                setResult(RESULT_OK, intent);
                serviceAdapter.setSelection(position);
                serviceAdapter.notifyDataSetChanged();
                ServiceContentsActivity.this.finish();
            }
        });
        recyclerView.setAdapter(serviceAdapter);
    }

    //业主工单 服务目录
    private void ownerSelectNewUnitHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(ServiceContentsActivity.this);
        hp.setUrl(Api.OWNER_QUERYDETAIL_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(1);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    //服务商 工单 服务目录
    private void serviceSelectNewUnitHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(ServiceContentsActivity.this);
        hp.setUrl(Api.SERVICE_QUERYDETAIL_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(2);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                Log.e("业主服务目录返回",result);
                OwnerContentsBean ownerBean = new Gson().fromJson(result,OwnerContentsBean.class);
                if (ownerBean.getMessage().getCode() == 200){
                    setOwnerListDate(ownerBean.getRows());
                }
                break;
            case 2:
                Log.e("服务商返回",result);
                ServiceContentsBean serviceBean = new Gson().fromJson(result,ServiceContentsBean.class);
                if (serviceBean.getMessage().getCode() == 200){
                    setServiceListDate(serviceBean.getRows());
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {

    }
}
